//
//  KOXMainTabBarController.swift
//  weibofirst
//
//  Created by Mars on 16/4/8.
//  Copyright © 2016年 Mars. All rights reserved.
//

import UIKit

class KOXMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //创建一份tabBar
        let MainTabBar  = XKOMainTabBar()
        
        //设置MainTabBar的闭包属性
        MainTabBar.composeClosure = { () -> Void in
        
            print("加号按钮被点击了")
        
        }
        
        //由于tabBar只读,所以使用kvc赋值
        setValue(MainTabBar, forKey: "tabBar")
        
          //创建Home->Item
//        let  HomeVC = XKOHomeTableViewController()
//        HomeVC.title = "首页"
//        HomeVC.tabBarItem.image = UIImage(named: "tabbar_home")
//        addChildViewController(HomeVC)
//        
//        //创建message->Item
//        let MessageVc = XKOMessageTableViewController()
//        MessageVc.title = "消息"
//        MessageVc.tabBarItem.image = UIImage(named: "tabbar_message_center")
//        addChildViewController(MessageVc)
//        
//        
//        let disCoverVC = XKODiscoverTableViewController()
//        disCoverVC.title = "搜索"
//        disCoverVC.tabBarItem.image = UIImage(named: "tabbar_discover")
//        addChildViewController(disCoverVC)
//        
//        
//        let profile = XKOProfileTableViewController()
//        profile.title = "我"
//        profile.tabBarItem.image = UIImage(named: "tabbar_profile")
//        addChildViewController(profile)
//        
        setUpMainTabBarItem()
        
    }

    
    //创建tabBarItem 

    private func setUpMainTabBarItem() {
    
        self.setUpTabBarItemVC(XKOHomeTableViewController(), XImageView: "tabbar_home", XtitleName: "首页")
        self.setUpTabBarItemVC(XKOMessageTableViewController(), XImageView: "tabbar_message_center", XtitleName: "消息")
        self.setUpTabBarItemVC(XKODiscoverTableViewController(), XImageView: "tabbar_discover", XtitleName: "发现")
        self.setUpTabBarItemVC(XKOProfileTableViewController(), XImageView: "tabbar_profile", XtitleName: "我")
    }
 
    //Mark:代码的抽取
    func setUpTabBarItemVC(XviewController:UIViewController , XImageView:String , XtitleName:String){
        
        //为Item设置图片
        XviewController.tabBarItem.image = UIImage(named: XImageView)
        
        XviewController.title = XtitleName
        
        XviewController.tabBarItem.selectedImage = UIImage(named:XImageView + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        XviewController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        
     // 创建一个导航控制器
       let navVC  = UINavigationController(rootViewController: XviewController)
  
       self.addChildViewController(navVC)
    
    }
    
}
